<?php
	namespace Common\Logic;
	use Common\Model\OrderRefundModel;
	class OrderRefundLogic extends OrderRefundModel{
		/*
		* Function:		getOrderRefundList
		* Description:	退款订单列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getOrderRefundList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getOrderRefundFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getOrderRefundFilter
		* Description:	退款订单列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getOrderRefundFilter($filter = ''){
			$conditon = array();
			if(!empty($filter['order_id'])){
				$conditon['order_id'] = $filter['order_id'];
			}
			if(!empty($filter['status'])){
				$conditon['status'] = $filter['status'];
			}
			if(!empty($filter['user_id'])){
				$conditon['user_id'] = $filter['user_id'];
			}
			if(!empty($filter['user_name'])){
				$wh = array();
				$wh['username'] = array('like','%'.$filter['user_name'].'%');
				$sql = D('User')->field('id')->where($wh)->select(false);
				$conditon['user_id'] = array('exp','in('.$sql.')');
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getOrderRefundCount
		* Description:	退款订单列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getOrderRefundCount($filter = ''){
			$conditon = $this->getOrderRefundFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addOrderRefund
		* Description:	增加退款订单
		* @param:		$data array 增加的数组
		*/
		public function addOrderRefund($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getOrderRefundInfo
		* Description:	获取退款订单信息 by row
		* @param:		$id int ID
		*/
		public function getOrderRefundInfo($id){
			return $this->where(array('id'=>$id))->find();
		}
		
		/*
		* Function:		getOrderRefundInfoByOrderId
		* Description:	获取退款订单信息 by row
		* @param:		$id int ID
		*/
		public function getOrderRefundInfoByOrderId($order_id){
			return $this->where(array('order_id'=>$order_id))->find();
		}
		
		/*
		* Function:		updateOrderRefund
		* Description:	更新退款订单
		* @param:		$data array 更新的数组
		*/
		public function updateOrderRefund($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteOrderRefund
		* Description:	删除退款订单
		* @param:		$id int ID
		*/
		public function deleteOrderRefund($id){
			return $this->where(array('id'=>$id))->delete();
		}
		
		/*
		* Function:		getOrderRefundAllList
		* Description:	部门列表
		*/
		public function getOrderRefundAllList(){
			return $this->where(array('status'=>1))->select();
		}
		
		/*
		* Function:		doRefund
		* param：$out_trade_no 订单号
		* param：$total_fee 退款金额
		* Description:	退款操作
		*/
		public function doRefund($out_trade_no,$total_fee){
			$parma = [
                'appid'         => C('appid'),  //appid
                'mch_id'        => C('mch_id'),  //商户id
                'nonce_str'     => wxNonceStr(),  //随机字符串
                'out_refund_no' => $out_trade_no.rand(0000,9999), //由后端生成的退款单号，需要保证唯一，因为多个同样的退款单号只会退款一次。
                'out_trade_no'  => $out_trade_no,                   //退款订单在支付时生成的订单号
                'total_fee'     => $total_fee*100,  //支付金额：此处单位是分所以需要*100
                'refund_fee'    => $total_fee*100,  //退款金额：此处单位是分所以需要*100
                'op_user_id'    => C('mch_id'),          //操作员 op_user_id .与商户号相同即可
            ];
            $parma['sign'] = wxSign($parma);
            $xmldata = arraytoxml($parma);
            $xmlresult = curl_post_ssl(C('wxRefundUrl'),$xmldata);
            $result = xmltoarray($xmlresult);
            if(!$result){
                return ['code'=>0,'errorMsg'=>'退款失败'];
            }
            if($result['result_code']!='SUCCESS'){
                return ['code'=>0,'errorMsg'=>'退款失败:'.$result['err_code_des']];
            }else{
                return ['code'=>1,'errorMsg'=>'退款成功'];
            }
		}
		
		/*
		* Function: generateOrderId
		* Description:生成唯一订单号
		* @param:$supplierCode string 商家编码orcode
		*/
		public function generateOrderId($code = ''){
			$order_id = $code.date('YmdHis',time()).rand(10000,99999);
			if($this->getOrderRefundInfoByOrderId($order_id)){
				return $this->generateOrderId($code);
			}
			return $order_id;
		}
	}
?>